package com.hanzhong.travel.service.impl;

import com.hanzhong.travel.entity.HanCultureActivity;
import com.hanzhong.travel.mapper.HanCultureActivityMapper;
import com.hanzhong.travel.service.HanCultureActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 汉文化活动服务实现类
 */
@Service
public class HanCultureActivityServiceImpl implements HanCultureActivityService {

    @Autowired
    private HanCultureActivityMapper activityMapper;

    @Override
    public boolean addActivity(HanCultureActivity activity) {
        return activityMapper.insert(activity) > 0;
    }

    @Override
    public boolean updateActivity(HanCultureActivity activity) {
        return activityMapper.update(activity) > 0;
    }

    @Override
    public boolean deleteActivity(Long id) {
        return activityMapper.deleteById(id) > 0;
    }

    @Override
    public HanCultureActivity getActivityById(Long id) {
        return activityMapper.findById(id);
    }

    @Override
    public List<HanCultureActivity> getAllActivities() {
        return activityMapper.findAll();
    }

    @Override
    public Map<String, Object> getActivitiesByPage(int page, int size) {
        // 计算偏移量
        int offset = (page - 1) * size;
        
        // 查询分页数据
        List<HanCultureActivity> activities = activityMapper.findByPage(offset, size);
        
        // 查询总数
        int total = activityMapper.count();
        
        // 封装结果
        Map<String, Object> result = new HashMap<>();
        result.put("activities", activities);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (int) Math.ceil((double) total / size));
        
        return result;
    }

    @Override
    public List<HanCultureActivity> getActivitiesByStatus(Integer status) {
        return activityMapper.findByStatus(status);
    }
} 